package cn.st4rlight.util.excel.reader;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;

/**
 * 用于easy-excel读取金额字符串时，自动处理千分位分隔符（即逗号）
 *
 * @author st4rlight <st4rlight@163.com>
 * Created on 2024-04-15
 */
public class MoneyAmountReader implements Converter<String> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        String data = context.getReadCellData().toString();
        if (StringUtils.isBlank(data)) {
            return data;
        }

        // excel中金额可能会被自动带上千分位分隔符（即逗号）
        return data.replace(",", "");
    }
}
